<?php
header('content-type:text/html charset:utf-8');
include_once '../../config.inc.php';

//生成日期时间+随机位数文件名
function upfilename($len){
	$tempstr = date('YmdHis',time()).'_';
	$chars = '0123456789abcdefghijklmnopqrstuvwxyz';
	$max = strlen($chars) - 1;
	PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
	for($i = 0; $i < $len; $i++) {
	$tempstr .= $chars[mt_rand(0, $max)];
	}
	return $tempstr;
}

    $dir_base = '../../upload/';     //文件上传根目录
    $dir_base1 = '../upload/';     	 //后台显示目录
    $dir_base2 = 'upload/';     	 //前台显示目录
    //没有成功上传文件，报错并退出。
    if(empty($_FILES)) {
        echo "<textarea><img src='{$dir_base}error.jpg'/></textarea>";
        exit(0);
    }
    
    $output = "<textarea>";
    $index = 0;        //$_FILES 以文件name为数组下标，不适用foreach($_FILES as $index=>$file)
    foreach($_FILES as $file){
        $upload_file_name = 'upload_file' . $index;        //对应index.html FomData中的文件命名
        $filename = $_FILES[$upload_file_name]['name'];
        //$gb_filename = iconv('utf-8','gb2312',$filename);    //名字转换成gb2312处理
        $gb_filename = upfilename(5).'.'.substr($_FILES[$upload_file_name]['name'],strripos($_FILES[$upload_file_name]['name'], '.')+1);
        
        //文件不存在才上传
        if(!file_exists($dir_base.$gb_filename)) {
            $isMoved = false;  //默认上传失败
            $MAXIMUM_FILESIZE = 2 * 1024 * 1024;     //文件大小限制    1M = 1 * 1024 * 1024 B;
            $rEFileTypes = "/^\.(jpg|jpeg|gif|png){1}$/i"; 
            if ($_FILES[$upload_file_name]['size'] <= $MAXIMUM_FILESIZE && 
                preg_match($rEFileTypes, strrchr($gb_filename, '.'))) {    
                $isMoved = @move_uploaded_file ( $_FILES[$upload_file_name]['tmp_name'], $dir_base.$gb_filename);        //上传文件
            }
        }else{
            $isMoved = true;    //已存在文件设置为上传成功
        }
        if($isMoved){
            //输出图片文件<img>标签
            //注：在一些系统src可能需要urlencode处理，发现图片无法显示，
            //    请尝试 urlencode($gb_filename) 或 urlencode($filename)，不行请查看HTML中显示的src并酌情解决。
            $output .= "<img src='{$dir_base1}{$gb_filename}' title='{$gb_filename}' alt='{$dir_base2}{$gb_filename}'/>";
        }else {
            $output .= "<img src='{$dir_base1}error.jpg' title='{$gb_filename}' alt='{$dir_base2}{$gb_filename}'/>";
        }
        
        $index++;
    }
    
    echo $output."</textarea>";
    
//End_php












//if (isset($_FILES["myfile"])) {
//	$ret = array();
//	//$uploadDir = 'images'.DIRECTORY_SEPARATOR.date("Ymd").DIRECTORY_SEPARATOR;
//	//$uploadDir = '../../upload/' . DIRECTORY_SEPARATOR;
//	$uploadDir = ROOT.'upload/';
//	$dir = dirname(__FILE__) . '/' . $uploadDir;
//	file_exists($dir) || (mkdir($dir, 0777, true) && chmod($dir, 0777));
//	if (!is_array($_FILES["myfile"]["name"]))//single file
//	{
//		//$fileName = time().uniqid().'.'.pathinfo($_FILES["myfile"]["name"])['extension'];
//		//$fileName = $_FILES["myfile"]["name"];
//		//$filename =  ROOT.'upload/'.upfilename(5).'.'.substr($_FILES['upfile']['name'],strripos($_FILES['upfile']['name'], '.')+1);
//		$filename =  upfilename(5).'.'.substr($_FILES['upfile']['name'],strripos($_FILES['upfile']['name'], '.')+1);
//		move_uploaded_file($_FILES["myfile"]["tmp_name"], $dir . $fileName);
//		$ret['file'] = DIRECTORY_SEPARATOR . $uploadDir . $fileName;
//	}
//	echo json_encode($ret);
//}
?>